package com.example.pei.ithinklearnt.adapter;


import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.anenn.flowlikeviewlib.FlowLikeView;

import com.example.pei.ithinklearnt.R;
import com.example.pei.ithinklearnt.bean.AlumniCircle;
import com.example.pei.ithinklearnt.bean.UserInfo;
import com.example.pei.ithinklearnt.view.activity.FrescoLoaderPicturesActivity;
import com.example.pei.ithinklearnt.view.customview.JustifyTextView;
import com.jaeger.ninegridimageview.NineGridImageView;
import com.jaeger.ninegridimageview.NineGridImageViewAdapter;
import com.makeramen.roundedimageview.RoundedImageView;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import static com.example.pei.ithinklearnt.utils.ImageLoaderUtil.displayImage;
import static com.example.pei.ithinklearnt.utils.ImageLoaderUtil.getPhotoImageOption;
import static com.example.pei.ithinklearnt.utils.ImageLoaderUtil.getPhotoImageOption2;
import static com.example.pei.ithinklearnt.utils.MyUtils.getSendTime;


// TODO: 2018/1/17    在刷新或者加载更多的时候 点一个动态进去，返回后，下面的带图片的动态 图片会变成白色
public class NineGridTestAdapter extends RecyclerView.Adapter<NineGridTestAdapter.ViewHolder> implements View.OnClickListener,View.OnLongClickListener{

    public static final int TYPE_HEADER = 0;  //说明是带有Header的
    public static final int TYPE_FOOTER = 1;  //说明是带有Footer的
    public static final int TYPE_NORMAL = 2;  //说明是不带有header和footer的

    private Context mContext;
    private List<AlumniCircle> mList;
    private LayoutInflater inflater;

    private View mHeaderView;
    private View mFooterView;

    //
    private itemListener mItemListener;
    public  interface itemListener{
        void onThreePointClick(View v,int position);
        void onHeartClick(View v,int position);
        void onItemClick(View view , int position);
        void onItemLongClick(View view , int position);
        void onHeadClick(View v,int position);
    }

    public NineGridTestAdapter(Context context ,itemListener mItemListener) {
        mContext = context;
        inflater = LayoutInflater.from(context);
        mList = new ArrayList<>();
        this.mItemListener=mItemListener;

    }
    public List<AlumniCircle> getmList(){return mList;}
    public void clearDate(){
        mList.clear();
    }
    public void setList(List<AlumniCircle> list) {
        mList = list;
    }


    //添加到第一个位置
    public void addDatetoFoot(AlumniCircle newdata) {
        mList.add(newdata);
    }
    //HeaderView和FooterView的get和set函数
    public View getHeaderView() {
        return mHeaderView;
    }
    public void setHeaderView(View headerView) {
        mHeaderView = headerView;
        notifyItemInserted(0);
    }
    public View getFooterView() {
        return mFooterView;
    }
    public void setFooterView(View footerView) {
        mFooterView = footerView;
        notifyItemInserted(getItemCount()-1);
    }

    @Override
    public void onClick(View view) {
        mItemListener.onItemClick(view,(int)view.getTag());
    }

    @Override
    public boolean onLongClick(View view) {
        mItemListener.onItemLongClick(view,(int)view.getTag());
        return false;
    }

    //创建View，如果是HeaderView或者是FooterView，直接在Holder中返回
    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        if(mHeaderView != null && viewType == TYPE_HEADER) {
            return new ViewHolder(mHeaderView);
        }
        if(mFooterView != null && viewType == TYPE_FOOTER){
            return new ViewHolder(mFooterView);
        }
        View convertView = inflater.inflate(R.layout.item_bbs_nine_grid, parent, false);
        convertView.setOnClickListener(this);      //设置监听
        convertView.setOnLongClickListener(this);  //设置长按监听
        return new ViewHolder(convertView);

    }
    @Override
    public void onBindViewHolder(final ViewHolder holder, @SuppressLint("RecyclerView") final int position) {

        if(getItemViewType(position) == TYPE_NORMAL){
            UserInfo sender=mList.get(position).getSender();

            if (mHeaderView == null) {
                //设置九宫格图片
                holder.mNglContent.setImagesData(mList.get(position).getphotos(),NineGridImageView.STYLE_GRID);
                //设置发送者昵称
                holder.textView_sendName.setText(sender.getNickName());
                //设置发送时间
                holder.textView_sendTime.setText(getSendTime(mList.get(position).getCreatedAt()));
                //设置发送文字
                if(mList.get(position).getContent()==null||mList.get(position).getContent().equals("")){
                    holder.textView_sendContent.setVisibility(View.GONE);
                }else {
                    holder.textView_sendContent.setVisibility(View.VISIBLE);
                    holder.textView_sendContent.setText((mList.get(position).getContent()));
                }
                //设置发送者头像
//                String path=mContext.getExternalCacheDir()+"/heads/"+"head-"+mList.get(position).getSenderID()+".jpg";
//                Bitmap bitmap= BitmapFactory.decodeFile(path);
//                holder.roundedImageView_head.setImageBitmap(bitmap);
                displayImage(mContext,holder.roundedImageView_head,sender.getHeadFileUrl(),getPhotoImageOption2());

                //设置发送者性别
                if(sender.getSex()==1) {
                    holder.imageView_sex.setBackgroundResource(R.drawable.male);
                }else if(sender.getSex()==0){
                    holder.imageView_sex.setBackgroundResource(R.drawable.female);
                }
                //设置菜单按钮监听
                holder.imageView_selected.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        mItemListener.onThreePointClick(view,position);
                    }
                });
                holder.roundedImageView_head.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        mItemListener.onHeadClick(v,position);
                    }
                });
                //设置点赞个数
                int num=mList.get(position).getPraiseNumber();
                if(num<999){
                    holder.textView_praiseNum.setText(num+"喜欢  ");
                }else {
                    holder.textView_praiseNum.setText("999+    ");
                }
                //设置评论个数
                holder.textView_commentNum.setText("  "+mList.get(position).getCommentNumber()+"评论");
                //设置爱心的样式
                holder.textView_heart.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        mItemListener.onHeartClick(view,position);
                        holder.likeViewLayout.addLikeView();
                    }
                });
                if(mList.get(position).getIsPraise()==0){
                    holder.textView_heart.setTextColor(Color.parseColor("#b1c6d7"));
                    holder.textView_heart.setTextSize(15);
                    holder.textView_praiseNum.setTextColor(Color.parseColor("#828282"));
                }else {
                    holder.textView_heart.setTextColor(Color.parseColor("#FC81AF"));
                    holder.textView_heart.setTextSize(18);
                    holder.textView_praiseNum.setTextColor(Color.parseColor("#FC81AF"));
                }
                holder.itemView.setTag(position);    //标记item 用来检测哪个item被点击了

            }else{
                //这里加载数据的时候要注意，是从position-1开始，因为position==0已经被header占用了
                // holder.mNglContent.setImagesData(mList.get(position-1).getphotos(),NineGridImageView.STYLE_GRID);
                // holder.textView_sendContent.setText(mList.get(position-1).getContent());
            }
        }else if(getItemViewType(position) == TYPE_HEADER){
            return;
        }else{
            return;
        }
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position, List<Object> payloads) {
        super.onBindViewHolder(holder, position, payloads);
        if(payloads.isEmpty()){
            onBindViewHolder(holder,position);
        }else{
            //设置点赞个数
            int num=mList.get(position).getPraiseNumber();
            if(num<999){
                holder.textView_praiseNum.setText(num+"喜欢  ");
            }else {
                holder.textView_praiseNum.setText("999+    ");
            }
            holder.textView_praiseNum.setTextColor(Color.parseColor("#FC81AF"));
            //Like 字体变化
            holder.textView_heart.setTextColor(Color.parseColor("#FC81AF"));
            holder.textView_heart.setTextSize(18);
        }
    }

    /** 重写这个方法，很重要，是加入Header和Footer的关键，我们通过判断item的类型，从而绑定不同的view    * */
    @Override
    public int getItemViewType(int position) {

        if (position == 0 && mHeaderView != null)
            return TYPE_HEADER;
        if (position == getItemCount() - 1 && mFooterView != null)
            return TYPE_FOOTER;
        return TYPE_NORMAL;

    }

    @Override
    public int getItemCount() {

        //个数计算的是getListSize(mList) 即有几个动态有图片，应该失算有几个动态
        //NineGridTestModel  应该用table里面的那个类
        int size = getListSize(mList) + 2;
        if (mHeaderView == null) {
            size -= 1;
        }
        if (mFooterView == null) {
            size -= 1;
        }
        return size;

    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        NineGridImageView<String> mNglContent;
        TextView textView_sendName;
        JustifyTextView textView_sendContent;
        TextView textView_sendTime;
        RoundedImageView  roundedImageView_head;
        ImageView imageView_sex;
        ImageView imageView_selected;       //三点
        TextView textView_heart;
        TextView textView_praiseNum;
        TextView textView_commentNum;

        FlowLikeView likeViewLayout;



        NineGridImageViewAdapter<String> mAdapter = new NineGridImageViewAdapter<String>() {
            @Override
            protected void onDisplayImage(Context context, ImageView imageView, String s) {
                displayImage(context, imageView, s, getPhotoImageOption());
            }

            @Override
            protected ImageView generateImageView(Context context) {
                return super.generateImageView(context);
            }

            @Override
            protected void onItemImageClick(Context context, ImageView imageView, int index, List<String> list) {
                ArrayList<String> arrayList = (ArrayList<String>) list;
                Intent intent=new Intent(context,FrescoLoaderPicturesActivity.class);
                intent.putStringArrayListExtra("photoUris",arrayList);
                intent.putExtra("currentPage",index);
                context.startActivity(intent);

            }

            @Override
            protected boolean onItemImageLongClick(Context context, ImageView imageView, int index, List<String> list) {
                //  Toast.makeText(context, "image long click position is " + index, Toast.LENGTH_SHORT).show();
                return true;
            }
        };

        public ViewHolder(View itemView) {
            super(itemView);
            //如果是headerview或者是footerview,直接返回
            if (itemView == mHeaderView){
                return;
            }
            if (itemView == mFooterView){
                return;
            }
            mNglContent =  itemView.findViewById(R.id.ngl_images);
            mNglContent.setAdapter(mAdapter);

            textView_sendName=itemView.findViewById(R.id.tv_sendname);
            textView_sendContent=itemView.findViewById(R.id.tv_sendcontent);
            textView_sendTime=itemView.findViewById(R.id.tv_sendtime);
            roundedImageView_head=itemView.findViewById(R.id.img_roudhead);
            imageView_sex=itemView.findViewById(R.id.iv_sex);
            imageView_selected=itemView.findViewById(R.id.iv_selected);
            textView_heart= itemView.findViewById(R.id.tv_heart);
            textView_praiseNum=itemView.findViewById(R.id.tv_praisenum);
            textView_commentNum=itemView.findViewById(R.id.tv_commentnum);

            likeViewLayout =  itemView.findViewById(R.id.flowLikeView);
            //   bubbleView=(BubbleView)itemView.findViewById(R.id.praise_anim);
            //    bubbleView.setDefaultDrawableList();
        }
    }

    private int getListSize(List<AlumniCircle> list) {
        if (list == null || list.size() == 0) {
            return 0;
        }
        return list.size();
    }


}
